﻿using Pivotbrowser.Web;
using Pivotbrowser.Web.Services;
using System;
using System.Collections.ObjectModel;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;
using System.Linq;

namespace Pivotbrowser.Models
{
    public class ViewModel
    {
        public ObservableCollection<MinimalStudent> MinimalStudents { get; set; }

        public ViewModel()
        {
            MinimalStudents = new ObservableCollection<MinimalStudent>();
        }

        public void LoadMinimalStudents()
        {
            MinimalStudents.Clear();
            var context = new ContentDomainContext();
            var query0 = context.GetSemestersQuery();
            context.Load(query0, (loadOp0) =>
            {                 
                var currentsemesters = loadOp0.Entities.OrderByDescending(x => Int32.Parse(x.SEMESTERNR)).Take(3).ToList();
                var query = context.GetStudentsQuery();
                context.Load(query, (loadOp) =>
                {
                    foreach (Student s in loadOp.Entities)
                    {
                        var ms = Factory.BuildMinimalStudent(s, currentsemesters);
                        if (ms != null)
                            MinimalStudents.Add(ms);
                        //GenerateDummies(20);
                    }
                    MinimalStudentsLoaded(this, new EventArgs());
                },
                null); 
            }, null);
        }


        private void GenerateDummies(int i)
        {
            for (int a = 0; a < i; a++)
            {                
                List<PrüfungStudent> newtests = new List<PrüfungStudent>();
                for (int b = 0; b < 20; b++)
                {
                    var newtest = new PrüfungStudent()
                    {
                        ZEUGNISBEZ = "Dummy " + b,
                        SEMESTERNR = "80",
                        SEMESTERBEZ = "DD 47/11",
                        NOTE_ = (b % 5).ToString() + ".0",
                        STUDFACH = "Bachelor Winfo",
                        TITEL = "Titellos",
                        PRFBEMNR = "Bestanden",
                        BESTANDEN = "1"
                    };
                    newtests.Add(newtest);
                }

                var newstud = new MinimalStudent()
                {
                    MATRNR = "2525566",
                    NACHNAME = "Dummy",
                    VORNAME = "Test",
                    ECTSGESAMT = 42.00,
                    AKTIV = "Nein",
                    FACHSEMESTER = 10,
                    GEBDATUM = DateTime.Now,
                    JUNIORSTUDIUM = "war nie Juniorstudent",
                    SEX = "männlich",
                    SexColor = new SolidColorBrush(Colors.LightGray),
                    ORT = "Dummy",
                    STRASSE = "Dummy",
                    PLZ = "123456",
                    TELEFONNR = "0800",
                    MAIL = "dummy@dummy.de",
                    MAIL2 = "dummy@dummy.de",
                    NOTIZEN = "hat keine Notiz",
                    NOTIZ = "",
                    STUDFACH = "Bachelor Winfo",
                    MarkerColor = new SolidColorBrush(Colors.Transparent),
                    MARKER = "Keine Markierung",
                    p_collection = newtests,
                    AltePrüfungen = newtests
                };

                this.MinimalStudents.Add(newstud);
            }
        }


        public event MinimalStudentsLoadedEventHandler MinimalStudentsLoaded;
        public delegate void MinimalStudentsLoadedEventHandler(object sender, EventArgs eargs);

    }
}
